package com.easemob.chatui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import com.easemob.chatui.adapter.TaskAdapter;
import com.google.gson.Gson;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.bean.BmobUser;
import com.lyw.GeoLocationClient.json.Task;
import com.lyw.GeoLocationClient.service.BmobUserAdapter;
import com.lyw.GeoLocationClient.ui.GeotableItem;
import timber.log.Timber;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.easemob.chatui.activity.NewTaskActivity.startNewTaskActivityActivityForResult;
import static com.easemob.chatui.activity.TaskInfoActivity.starTaskInfoActivityActivityForResult;

public class TaskActivity extends BaseActivity {
    private static final String USERNAME = "username";
    private ListView mList;
    private Button mButton;
    private ProgressBar mProgress;
    TaskAdapter taskAdapter;
    private BmobUserAdapter mBmobUserAdapter;
    private BmobUser mSelectedUser;
    private ArrayList<Task> mTaskList;

    public static void startTaskActivityForResult(Activity context, String name, int code) {
        Intent intent = new Intent();
        intent.putExtra(USERNAME, name);
        intent.setClass(context, TaskActivity.class);

        context.startActivityForResult(intent, code);
    }

    public static void startTaskActivity(Context context, String name) {
        Intent intent = new Intent();
        intent.putExtra(USERNAME, name);
        intent.setClass(context, TaskActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_task);

        String mUserName = getIntent().getStringExtra(USERNAME);
        if (mUserName == null) {
            Toast.makeText(this, "unknown user", Toast.LENGTH_SHORT).show();
            this.finish();
            return;
        }

        mBmobUserAdapter = new BmobUserAdapter(this);

        mList = (ListView) findViewById(R.id.task_list);
        mButton = (Button) findViewById(R.id.btn_new_task);
        mProgress = (ProgressBar) findViewById(R.id.progress);

        taskAdapter = new TaskAdapter(this, null);
        mList.setAdapter(taskAdapter);

        if (App.isAdmin()) {
            mButton.setText(R.string.add_task);
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startNewTaskActivity();
                }
            });
        } else {
            mButton.setVisibility(View.GONE);
        }
        getUserList(mUserName);


        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mSelectedUser != null) {
                    starTaskInfoActivityActivityForResult(TaskActivity.this, mSelectedUser, i);
                }
            }
        });
    }


    private void getUserList(final String user) {
        mProgress.setVisibility(View.VISIBLE);

        mBmobUserAdapter.queryBmobUser(user, new FindListener<BmobUser>() {
            @Override
            public void onSuccess(List<BmobUser> list) {
                if (list != null && list.size() > 0) {
                    Timber.d("get users size " + list.size());
                    mSelectedUser = list.get(0);

                    bindDataFromUser(mSelectedUser);
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    private void bindDataFromUser(BmobUser user) {
        ArrayList<String> stringArrayList = user.getTasks();
        if (mTaskList == null) {
            mTaskList = new ArrayList<Task>();
        }
        mTaskList.clear();
        mProgress.setVisibility(View.GONE);
        if (stringArrayList == null) {
            return;
        }

        Gson gson = new Gson();
        for (int i = 0, stringArrayListSize = stringArrayList.size(); i < stringArrayListSize; i++) {
            String t = stringArrayList.get(i);
            Timber.d("get ori task " + t);

            Task task = gson.fromJson(t, Task.class);
            Timber.d("get task " + task.toString());
            mTaskList.add(task);
        }
//        Collections.reverse(mTaskList);
        taskAdapter.update(mTaskList);

    }

    private void startNewTaskActivity() {
        startNewTaskActivityActivityForResult(this, mSelectedUser, NewTaskActivity.REQUEST_CODE_TASK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            mSelectedUser = (BmobUser) data.getSerializableExtra("user");
            Timber.d("get user from new task " + mSelectedUser.toString());

            mSelectedUser.update(this, new UpdateListener() {
                @Override
                public void onSuccess() {
                    bindDataFromUser(mSelectedUser);
                    Timber.d("update task ok");
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(TaskActivity.this, "failed", Toast.LENGTH_LONG).show();
                }
            });
        } else {

        }
    }
}
